#include "feedparser.h"
#include "../NewSyndrome-module.h"
#include <stdlib.h>
#include <string.h>


void free_feed (feed_t **mfeed)
{
	int i;
	
	for (i = 0; mfeed[i]; i++)
	{
		free (mfeed[i]->link);
		free (mfeed[i]->desc);
		free (mfeed[i]->title);
		free (mfeed[i]);
	}
	
	free (mfeed);
}


int isatom (const char *data)
{
	char *pdebut, *pfin;
	
	pfin = strstr (data, "<entry");
	if (!pfin)
		return (0);
	
	pdebut = strstr (data, "<feed xml");
	if (!pdebut)
		return (0);
	
	return (1);
}


int isrss (const char *data)
{
	char *pdebut, *pfin;
	
	pfin = strstr (data, "<item");
	if (!pfin)
		return (0);
	
	pdebut = strstr (data, "<rss");
	if (!pdebut)
		return (0);
	
	return (1);
}


feed_t **parse_atom (const char *data, const char *last_title)
{
	int nfeed = 0, n;
	char *pdesc, *plink, *ptitle, *pdata, *pitem, *pnext_item;
	char *pfdesc, *pflink, *pftitle;
	
	feed_t **pfeed = NULL;
	
	if (!data)
		return (NULL);
	
	pdata = (char *) data;
  
	while (1)
	{
		pfeed = (feed_t **) realloc (pfeed, sizeof (feed_t *) * (nfeed + 1));
		pfeed[nfeed] = (feed_t *) malloc (sizeof (feed_t));
		pfeed[nfeed]->link = NULL;
		pfeed[nfeed]->desc = NULL;
		pfeed[nfeed]->title = NULL;
	  
		pitem = strstr (pdata, "<entry");
		if (!pitem)
			break;
		pdata = pitem + 6;
		
		pnext_item = strstr (pdata, "<entry"); 
	
		ptitle = strstr (pitem, "<title>");
		if (!ptitle)
			break;
		if (pnext_item && ptitle > pnext_item)
			break;
		ptitle += 7;
		pftitle = strstr (ptitle, "</title>");
		if (!pftitle)
			break;
	
		n = pftitle - ptitle;
		pfeed[nfeed]->title = malloc (n + 1);
		strncpy (pfeed[nfeed]->title, ptitle, n);
		pfeed[nfeed]->title[n] = '\0';
		pfeed[nfeed]->link = NULL;
		
		if (last_title && !strcmp (pfeed[nfeed]->title, last_title))
			break;

		plink = strstr (pitem, "<link");
		if (plink)
		{
			if (pnext_item && plink > pnext_item)
				break;
			plink += 5;
			plink = strstr (plink, "href=\"");
			if (plink)
			{
				plink += 6;
				pflink = strchr (plink, '\"');
				if (pflink)
				{
					n = pflink - plink;
					pfeed[nfeed]->link = malloc (n + 1);
					strncpy (pfeed[nfeed]->link, plink, n);
					pfeed[nfeed]->link[n] = '\0';
				}
			}
		}
		
		if (pfeed[nfeed]->link == NULL)
		{
			pfeed[nfeed]->link = malloc (1);
			pfeed[nfeed]->link[0] = '\0';
		}
		
		pdesc = strstr (pitem, "<description");
		if (pdesc)
		{
			if (pnext_item && pdesc > pnext_item)
				break;
				
			pdesc += 12;
			pdesc = strchr (pdesc, '>');
			if (!pdesc)
				break;
			
			pfdesc = strstr (pdesc, "</description");
			if (!pfdesc)
				break;
		
			n = pfdesc - pdesc;
			pfeed[nfeed]->desc = malloc (n + 1);
			strncpy (pfeed[nfeed]->desc, pdesc, n);
			pfeed[nfeed]->desc[n] = '\0';
		}
		else
		{
			pdesc = strstr (pitem, "<content");
			if (pdesc)
			{
				if (pnext_item && pdesc > pnext_item)
					break;
				
				pdesc += 8;
				pdesc = strchr (pdesc, '>');
				if (!pdesc)
					break;
				
				pfdesc = strstr (pdesc, "</content");
				if (!pfdesc)
					break;
			
				n = pfdesc - pdesc;
				pfeed[nfeed]->desc = malloc (n + 1);
				strncpy (pfeed[nfeed]->desc, pdesc, n);
				pfeed[nfeed]->desc[n] = '\0';
			}
		}
		if (!pdesc)
		{
			pfeed[nfeed]->desc = malloc (1);
			pfeed[nfeed]->desc[0] = '\0';
		}
		
		pdata = strstr (pitem, "</entry>");
		if (!pdata)
			break;
		
		nfeed++;
	}

	if (pfeed[nfeed]->title)
		free (pfeed[nfeed]->title);
	if (pfeed[nfeed]->link)
		free (pfeed[nfeed]->link);
	if (pfeed[nfeed]->desc)
		free (pfeed[nfeed]->desc);
	if (pfeed[nfeed])
		free (pfeed[nfeed]);
	pfeed[nfeed] = NULL;
  
	return (pfeed);
}


feed_t **parse_rss (const char *data, const char *last_title)
{
	int nfeed = 0, n;
	char *pdesc, *plink, *ptitle, *pdata, *pitem;
	char *pfdesc, *pflink, *pftitle;
	
	feed_t **pfeed = NULL;
	
	if (!data)
		return (NULL);
	
	pdata = (char *) data;
  
	while (1)
	{
		pfeed = (feed_t **) realloc (pfeed, sizeof (feed_t *) * (nfeed + 1));
		pfeed[nfeed] = (feed_t *) malloc (sizeof (feed_t));
		pfeed[nfeed]->link = NULL;
		pfeed[nfeed]->desc = NULL;
		pfeed[nfeed]->title = NULL;
	  
		while (1)
		{
			pitem = strstr (pdata, "<item");
			if (!pitem)
				break;
			if (pitem[5] == '>' || pitem[5] == ' ')
				break;
			pdata = pitem + 5;
		}
		if (!pitem)
			break;
	
		ptitle = strstr (pitem, "<title>");
		if (!ptitle)
			break;
		ptitle += 7;
		pftitle = strstr (ptitle, "</title>");
		if (!pftitle)
			break;
	
		n = pftitle - ptitle;
		pfeed[nfeed]->title = malloc (n + 1);
		strncpy (pfeed[nfeed]->title, ptitle, n);
		pfeed[nfeed]->title[n] = 0;
		
		if (last_title && !strcmp (pfeed[nfeed]->title, last_title))
			break;

		plink = strstr (pitem, "<link>");
		if (plink)
		{
			plink += 6;
			pflink = strstr (plink, "</link>");
			if (!pflink)
				break;
		
			n = pflink - plink;
			pfeed[nfeed]->link = malloc (n + 1);
			strncpy (pfeed[nfeed]->link, plink, n);
			pfeed[nfeed]->link[n] = 0;
		}
		else
		{
			pfeed[nfeed]->link = malloc (1);
			pfeed[nfeed]->link[0] = 0;
		}
		pdesc = strstr (pitem, "<description>");
		if (pdesc)
		{
			pdesc += 13;
			pfdesc = strstr (pdesc, "</description>");
			if (!pfdesc)
				break;
		
			n = pfdesc - pdesc;
			pfeed[nfeed]->desc = malloc (n + 1);
			strncpy (pfeed[nfeed]->desc, pdesc, n);
			pfeed[nfeed]->desc[n] = 0;
		}
		else
		{
			pfeed[nfeed]->desc = malloc (1);
			pfeed[nfeed]->desc[0] = 0;
		}
		
		pdata = strstr (pitem, "</item>");
		if (!pdata)
			break;
		
		nfeed++;
	}

	if (pfeed[nfeed]->title)
		free (pfeed[nfeed]->title);
	if (pfeed[nfeed]->link)
		free (pfeed[nfeed]->link);
	if (pfeed[nfeed]->desc)
		free (pfeed[nfeed]->desc);
	if (pfeed[nfeed])
		free (pfeed[nfeed]);
	pfeed[nfeed] = NULL;
  
	return (pfeed);
}
